'use stict'

const Koa = require('koa')
const router = require('koa-router')()
const bodyparser = require('koa-bodyparser')
const cors = require('koa-cors')

const { Sequelize, DataTypes, Op } = require('sequelize')

let sequelize = new Sequelize('test_db', 'postgres', 'llb1106', {
    host: 'llb1106.top',
    dialect: 'postgres'
})

let Blogs = sequelize.define('blogs', {
    title: {
        type: DataTypes.STRING,
        allowNull: false
    },
    summary: {
        type: DataTypes.STRING,
        allowNull: false
    },
    content: {
        type: DataTypes.STRING,
        allowNull: false
    },
    sort: {
        type: DataTypes.STRING,
        allowNull: false
    },
    author: {
        type: DataTypes.STRING,
        allowNull: false
    }
})

sequelize.sync({ force: true }).then(function () {
    Blogs.bulkCreate([
        {
            title: "论EFCore的自我修养",
            summary: "论EFCore的自我修养",
            content: "论EFCore的自我修养",
            sort: ".Net",
            author: "InCerry",
        },
        {
            title: "DDD之我见",
            summary: "DDD之我见",
            content: "DDD之我见",
            sort: "编程技术",
            author: "某大神",
        },
    ])
})

let app = new Koa()

router.get('/blog', async (ctx, next) => {
    let keyword = ctx.request.query.keyword || '';
    if (keyword) {
        ctx.body = await Blogs.findAll({
            where: {
                [Op.or]: [
                    { id: isNaN(keyword) ? 0 : parseInt(keyword) },
                    { title: keyword },
                    { summary: keyword },
                    { content: keyword },
                    { sort: keyword },
                    { author: keyword },
                ]
            },
            order: ['id']
        })
    } else {
        ctx.body = await Blogs.findAll({
            order: ['id']
        })
    }
})

router.delete('/blog/:id', async (ctx, next) => {
    let id = ctx.request.params.id;
    await Blogs.destroy({
        where: { id: id }
    })
    ctx.body = await Blogs.findAll({
        order: ['id']
    })
})

router.get('/blog/:id', async (ctx, next) => {
    let id = ctx.request.params.id;
    ctx.body = await Blogs.findByPk(id)
})

router.put('/blog', async (ctx, next) => {
    let obj = ctx.request.body;
    if (obj.id) {
        await Blogs.update(
            {
                title: obj.title,
                summary: obj.summary,
                content: obj.content,
                sort: obj.sort,
                author: obj.author,
            },
            {
                where: {
                    id: obj.id
                }
            }
        )
        ctx.body = {
            code: 1000,
            msg: '编辑成功'
        }
    }
})

router.post('/blog', async (ctx, next) => {
    let obj = ctx.request.body;
    await Blogs.create({
        title: obj.title,
        summary: obj.summary,
        content: obj.content,
        sort: obj.sort,
        author: obj.author,
    })
    ctx.body = {
        code: 1000,
        msg: '添加成功'
    }
})

app.use(cors())
app.use(bodyparser())
app.use(router.routes())

app.listen(8000)
console.log('服务器运行在如下地址：http://localhost:8000');